package com.example.yichuangyun.mvvm.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.DataBindingHolder;
import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemOrderListBinding;
import com.example.yichuangyun.model.order.OrderListDataModel;

import java.util.ArrayList;
import java.util.List;

public class OrderListAdapter extends BaseQuickAdapter<OrderListDataModel.OrderRecordsModel, DataBindingHolder<ItemOrderListBinding>> {

    private final Context context;

    public OrderListAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemOrderListBinding> holder, int position, @Nullable OrderListDataModel.OrderRecordsModel item, @NonNull List<?> payloads) {
        super.onBindViewHolder(holder, position, item, payloads);
        ItemOrderListBinding binding = holder.getBinding();
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position, item);
        } else {
            if (null != item) {
                binding.llPaymentConfirm.setEnabled(item.getMoneyConfirmStatus() != 1);
                binding.tvPaymentConfirmText.setTextColor(context.getColor(item.getMoneyConfirmStatus() == 1 ? R.color.color_6A84A2 : R.color.color_426FFF));
            }
        }
    }

    @Override
    protected void onBindViewHolder(@NonNull DataBindingHolder<ItemOrderListBinding> itemOrderListBindingDataBindingHolder, int i, @Nullable OrderListDataModel.OrderRecordsModel orderRecordsModel) {
        ItemOrderListBinding binding = itemOrderListBindingDataBindingHolder.getBinding();
        binding.setOrderModel(orderRecordsModel);
        if (orderRecordsModel != null) {
            int color = context.getColor(R.color.color_426FFF);
            // 订单状态 -1已删除、0待激活、1服务中、2服务完成、3已关闭
            switch (orderRecordsModel.getOrderStatus()) {
                case -1:
                case 3:
                    color = context.getColor(R.color.color_BBC2CB);
                    break;
                case 0:
                    color = context.getColor(R.color.color_49C885);
                    break;
                case 1:
                    color = context.getColor(R.color.color_426FFF);
                    break;
                case 2:
                    color = context.getColor(R.color.color_6A84A2);
                    break;
            }
            binding.tvStatus.setTextColor(color);
            if (!TextUtils.isEmpty(orderRecordsModel.getSignDate())) {
                binding.tvSignDate.setText(context.getString(R.string.sign_time_mao,
                        orderRecordsModel.getSignDate().length() > 9
                                ? orderRecordsModel.getSignDate().substring(0, 10)
                                : orderRecordsModel.getSignDate()));
            }

            List<String> tags = new ArrayList<>();
            tags.add(context.getString(orderRecordsModel.getSource() == 0 ? R.string.online : R.string.offline));
            tags.add(orderRecordsModel.getRenewalState() == 0 ? context.getString(R.string.new_sign) : orderRecordsModel.getRenewalState() == 1 ? context.getString(R.string.renew) : "");
            tags.add(context.getString(orderRecordsModel.getServiceCategory() == 2 ? R.string.personal_work : R.string.company_work));
            binding.tagCloudView.setTags(tags);
            binding.tvCompanyName.setTextColor(context.getColor(orderRecordsModel.getEnterConfirmState() == 1 ? R.color.color_426FFF : R.color.color_7C7C7C));
            // 款项已确认/已关闭/已删除的订单不能再进行款项确认
            binding.llPaymentConfirm.setEnabled(orderRecordsModel.getMoneyConfirmStatus() != 1 && orderRecordsModel.getOrderStatus() != -1 && orderRecordsModel.getOrderStatus() != 3);
            binding.tvPaymentConfirmText.setTextColor(context.getColor(orderRecordsModel.getMoneyConfirmStatus() != 1 && orderRecordsModel.getOrderStatus() != -1 && orderRecordsModel.getOrderStatus() != 3 ? R.color.color_426FFF : R.color.color_6A84A2));
        }
    }

    @NonNull
    @Override
    protected DataBindingHolder<ItemOrderListBinding> onCreateViewHolder(@NonNull Context context, @NonNull ViewGroup viewGroup, int i) {
        return new DataBindingHolder<>(R.layout.item_order_list, viewGroup);
    }
}
